<?php

//mkMagicPlaylist();

//
// mkMagicPlaylist ()
// calculates tracks traffic events to play in m3u playlists at a later date
// for later generation.  Spins are based on song and album ratings, age, and format.
//
// inputs:
// 1. datetime for the playlist to start, in the format of "YYYY-MM-DD HH:mm:SS"
// 2. datetime for the playlist to end, in the format of "YYYY-MM-DD HH:mm:SS"
// 
// outputs:
//
// other tasks:
// inserts playlist structure into DB table `playlist`
// 
function mkMagicPlaylist ($playStart, $playEnd) {
   
   $playStart = strtotime($playStart);
   $playEnd = strtotime($playEnd);
   
   if ( ($playEnd - $playStart) < 0 ) {
      $playStartTemp = $playStart;
      $playStart = $playEnd;
      $playEnd = $playStartTemp;
   }
   
   if ( ($playEnd - $playStart) > 518400 ) {
      die('Sorry, but you cannot generate playlists more than 6 days long.<br />');
   }
   
   $playStartDay = date('N', $playStart);
   $playStartTime = date('H:i:s', $playStart);
   $playEndDay = date('N', $playEnd);
   $playEndTime = date('H:i:s', $playEnd);
   
   // 1 day = 86400 seconds, so we'll add that many seconds until
   // we reach the end date, and by such methods retrieve every 
   // weekday we need for generating the playlist
   $i=0;
   do {
      if (!$playDayArray) {
         $playDayArray[$i][datetime] = $playStart;
         $playDayArray[$i][weekday] = $playStartDay;
      } else {
         $playDayArray[$i][datetime] = $playDayArray[($i-1)][datetime] + 86400;
         $playDayArray[$i][weekday] = date('N', ($playDayArray[$i][datetime]));
      }
      echo('N='.$playDayArray[$i][weekday]);
   } while ($playDayArray[$i++][weekday] != $playEndDay);
   
   echo('<h3>'.date('Y-m-d H:i:s', $playStart).' / '.date('Y-m-d H:i:s', $playEnd).'</h3>');
   
   $progSchedSql = " SELECT * FROM `program` WHERE (`weekday`='".$playStartDay."') AND (`starttime`>='".$playStartTime."') ";
   for ($i=1; $i < count($playDayArray); $i++) {
      $progSchedSql .=" UNION SELECT * FROM `program` WHERE (`weekday`='".$playDayArray[$i][weekday]."') ";
   }
   $progSchedSql .= " AND (`starttime`<='".$playEndTime."') ";
   echo ($progSchedSql);
   $progSchedQuery = mysql_query($progSchedSql) or die(mysql_error());
   while ($progSched = mysql_fetch_array($progSchedQuery)) {
      printf('%s<br />', $progSched[starttime]);
   }
   $trafSchedQuery = mysql_query(" SELECT * FROM `schedule` ");

}

if ($do === 'startPlaylist') {
   $timestamp = $_REQUEST['startDate'].' '.$_REQUEST[startTime];
   mysql_query(" 
      INSERT INTO `playlist` 
      SET 
      `playtime`='".$timestamp."',
      `usr_id`='".$U_[id]."',
      `air_type`='5'
   ") or die(mysql_error());
}

mkMagicPlaylist('2006-09-10 18:40:40', '2006-09-15 12:11:10');

$playStartQuery = mysql_query(" SELECT `playtime` FROM `playlist` WHERE (`usr_id`='".$U_[id]."') AND (`air_type`='5') LIMIT 1 ");
//echo ($playStartQuery);
$playStartTime = @mysql_result($playStartQuery, 0);

if ($playStartTime) {
   ?>
   <form action="?mode=mkMagicPlaylist" method="post">
   <fieldset><legend>Append to Playlist</legend>
   <input type="radio" name="playType" value="" /> music only<br />
   <input type="radio" name="playType" value="" /> 
   <input type="radio" name="playType" value="" /> 
   <input type="radio" name="playType" value="" /> 
   <br />
   <input type="hidden" name="do" value="playlistAdd" />
   <input type="submit" value="Generate &gt;&gt;" />
   </fieldset>
   <br />
   <?php 
   $playEndTimeQuery = mysql_query(" SELECT `playtime` FROM `playlist` WHERE (`playtime`>'$playStartTime') AND ( (`air_type`='4') OR (`air_type`='5') ) ORDER BY `playtime` ASC LIMIT 1 ");
   $playEndTime = @mysql_result($playEndTimeQuery, 0);
   
   ?>Active Playlist<br /><?php    
   
   $playSql = " SELECT * FROM `playlist` WHERE (`playtime`>='".$playStartTime."') ";
   if ($playEndTime) $playSql .= " AND (`playtime`<='".$playEndTime."') ";
   $playQuery =  mysql_query($playSql) or die(mysql_error());
   ?><table><?php 
   while ($play = mysql_fetch_array($playQuery)) {
      if ($play[air_type] == 4 || $play[air_type] == 5) printf('<tr><td>%s: begin playlist</td></tr>', $play[playtime]);
      $playPlaytime = $play[playtime];
   }
   ?></table>
   <?php 
   $playTimeSec = strtotime($playPlaytime);
   if ($play[length]) {
      $playLengthSecArray = explode(':', $play[length]);
      if (count($playLengthSecArray) == 3) {
         $playLengthSec = ($playLengthSecArray[0] * 3600) + ($playLengthSecArray[1] * 60) + ($playLengthSecArray[2]);
      } else if (count($playLengthSecArray) == 2) {
         $playLengthSec = ($playLengthSecArray[0]*60) + $playLengthSecArray[1];
      } else if (count($playLengthSecArray) == 1) {
         $playLengthSec = $playLengthSecArray[0];
      }
   } else {
      $playLengthSec = 0;
   }
   
   //echo($playTimeSec);
   
   $playTimeStart = $playTimeSec + $playLengthSec;
   $playTimeStart = strftime('Y-m-d H:i:s', $playTimeStart);
   ?>
   <input type="hidden" name="startTime" value="<?=$playTimeStart?>" />(<?=$playTimeStart?>)
   </form><?php 
} else { 
   ?>
   <form action="?mode=mkMagicPlaylist" method="post">
   <fieldset><legend>Start Playlist Options</legend>
   Start Date: <input type="text" name="startDate" size="10" maxlength="10" /> (YYYY-MM-DD)<br />
   Start Time: <input type="text" name="startTime" size="8" maxlength="8" /> (HH:MM:SS, 24-hour time)<br />
   <input type="hidden" name="do" value="startPlaylist" />
   <input type="submit" value="Start Playlist &gt;&gt;" /></fieldset>
   </form>
   <?php 
}
?>